package com.jarvis.pzz.modules.shop.view;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.bumptech.glide.Glide;
import com.donkingliang.labels.LabelsView;
import com.jarvis.pzz.PhotoActivity;
import com.jarvis.pzz.R;
import com.jarvis.pzz.common.BaseActivity;
import com.jarvis.pzz.common.ResponseData;
import com.jarvis.pzz.models.AdvertisesmetListModel;
import com.jarvis.pzz.models.ShopDetailsModel;
import com.jarvis.pzz.models.ShopInfoModel;
import com.jarvis.pzz.models.ShopListModel;
import com.jarvis.pzz.models.ShopkeeperInfoModel;
import com.jarvis.pzz.modules.login.LoginActivity;
import com.jarvis.pzz.modules.releasedemand.adapter.ReleaseFacilitiesAdapter;
import com.jarvis.pzz.modules.shop.adapter.ShopLikeAdapter;
import com.jarvis.pzz.seervers.RequestService;
import com.jarvis.pzz.util.DipUtil;
import com.jarvis.pzz.util.IntentUtil;
import com.jarvis.pzz.util.net.RequestApi;
import com.jarvis.pzz.widget.BannerHeaderDetailView;
import com.jarvis.pzz.widget.BannerHeaderView;
import com.jarvis.pzz.widget.MyScrollView;
import com.jarvis.pzz.widget.ScrollGridView;
import com.jarvis.pzz.widget.ScrollListView;
import com.tbruyelle.rxpermissions.RxPermissions;
import com.umeng.analytics.MobclickAgent;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.functions.Action1;

public class ShopDetailsActivity extends BaseActivity implements View.OnClickListener,OnGetGeoCoderResultListener {
    @BindView(R.id.scroll)
    MyScrollView scroll;
    @BindView(R.id.title)
    RelativeLayout title;
    @BindView(R.id.banner)
    BannerHeaderDetailView banner;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.lin_title_middle)
    LinearLayout lin_title_middle;
    @BindView(R.id.lin)
    LinearLayout lin;
    @BindView(R.id.rel_left)
    RelativeLayout rel_left;
    @BindView(R.id.labels)
    LabelsView labelsView;

    @BindView(R.id.rel_collect)
    RelativeLayout rel_collect;
    @BindView(R.id.img_collect)
    ImageView img_collect;
    @BindView(R.id.tv_collect)
    TextView tv_collect;

    @BindView(R.id.tv_call)
    TextView tv_call;
    @BindView(R.id.tv_call_pbb)
    TextView tv_call_pbb;
    @BindView(R.id.tv_call_shop)
    TextView tv_call_shop;

    @BindView(R.id.rel_pyxx)
    RelativeLayout rel_pyxx;
    @BindView(R.id.tv_pyxx)
    TextView tv_pyxx;
    @BindView(R.id.lin_pyxx)
    LinearLayout lin_pyxx;
    @BindView(R.id.lin_address)
    LinearLayout lin_address;

    @BindView(R.id.rel_pybg)
    RelativeLayout rel_pybg;
    @BindView(R.id.tv_pybg)
    TextView tv_pybg;
    @BindView(R.id.lin_pybg)
    LinearLayout lin_pybg;
    @BindView(R.id.webView)
    WebView webView;

    @BindView(R.id.rel_jyzk)
    RelativeLayout rel_jyzk;
    @BindView(R.id.tv_jyzk)
    TextView tv_jyzk;
    @BindView(R.id.lin_jyzk)
    LinearLayout lin_jyzk;

    @BindView(R.id.tv_update_time)
    TextView tv_update_time;
    @BindView(R.id.tv_shopRentType)
    TextView tv_shopRentType;
    @BindView(R.id.tv_shopRent)
    TextView tv_shopRent;
    @BindView(R.id.tv_shopTransferFee)
    TextView tv_shopTransferFee;
    @BindView(R.id.tv_shopAvailableMeasure)
    TextView tv_shopAvailableMeasure;
    @BindView(R.id.tv_shopNo)
    TextView tv_shopNo;
    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.tv_shopIntroduce)
    TextView tv_shopIntroduce;
    @BindView(R.id.tv_shopType)
    TextView tv_shopType;
    @BindView(R.id.tv_shopSaleState)
    TextView tv_shopSaleState;
    @BindView(R.id.tv_shopFaceWidth)
    TextView tv_shopFaceWidth;
    @BindView(R.id.tv_shopFloor)
    TextView tv_shopFloor;
    @BindView(R.id.tv_shopCurrentBusiness)
    TextView tv_shopCurrentBusiness;
    @BindView(R.id.tv_area)
    TextView tv_area;
    @BindView(R.id.tv_shopBusinessArea)
    TextView tv_shopBusinessArea;
    @BindView(R.id.tv_shopAddress)
    TextView tv_shopAddress;
    @BindView(R.id.tv_shopStreet)
    TextView tv_shopStreet;
    @BindView(R.id.tv_shopPropertiyRight)
    TextView tv_shopPropertiyRight;
    @BindView(R.id.tv_shopBuildMeasure)
    TextView tv_shopBuildMeasure;
    @BindView(R.id.tv_shopAvailableMeasure_1)
    TextView tv_shopAvailableMeasure_1;
    @BindView(R.id.tv_shopDepth)
    TextView tv_shopDepth;
    @BindView(R.id.tv_shopFloorHeight)
    TextView tv_shopFloorHeight;
    @BindView(R.id.tv_shopCut)
    TextView tv_shopCut;
    @BindView(R.id.tv_shopRunState)
    TextView tv_shopRunState;
    @BindView(R.id.tv_shopRunYears)
    TextView tv_shopRunYears;
    @BindView(R.id.tv_shopBrandState)
    TextView tv_shopBrandState;
    @BindView(R.id.tv_shopRent_1)
    TextView tv_shopRent_1;
    @BindView(R.id.tv_shopRentType_1)
    TextView tv_shopRentType_1;
    @BindView(R.id.tv_shopDeposit)
    TextView tv_shopDeposit;
    @BindView(R.id.tv_shopRunIndustry_1)
    TextView tv_shopRunIndustry_1;
    @BindView(R.id.tv_shopCurrentLeaseYears)
    TextView tv_shopCurrentLeaseYears;
    @BindView(R.id.tv_shopSurplusYears)
    TextView tv_shopSurplusYears;
    @BindView(R.id.tv_shopLimitYears)
    TextView tv_shopLimitYears;
    @BindView(R.id.tv_shopRenew)
    TextView tv_shopRenew;
    @BindView(R.id.tv_shopTransferFee_1)
    TextView tv_shopTransferFee_1;
    @BindView(R.id.tv_shopTransferContent)
    TextView tv_shopTransferContent;
    @BindView(R.id.tv_shop_title)
    TextView tv_shop_title;
    @BindView(R.id.tv_shopProfit)
    TextView tv_shopProfit;
    @BindView(R.id.tv_leaseType)
    TextView tv_leaseType;

    @BindView(R.id.sc_labelList1)
    ScrollGridView sc_labelList1;
    @BindView(R.id.sc_like)
    ScrollListView sc_like;

    @BindView(R.id.img_photo)
    ImageView img_photo;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_no)
    TextView tv_no;
    @BindView(R.id.rel_call)
    RelativeLayout rel_call;

    @BindView(R.id.bmapView)
    MapView mMapView ;
    BaiduMap mBaiduMap = null;
    LocationClient mLocClient;
    LatLng ptCenter;
    LatLng myPt;
    public MyLocationListenner myListener = new MyLocationListenner();
    GeoCoder mSearch = null; // 搜索模块，也可去掉地图模块独立使用
    private Marker myMarker;

    private String shopId;
    private RequestService service;
    private ShopDetailsModel model;
    private ShopkeeperInfoModel shopkeeperInfo;
    private ShopInfoModel shopInfo;
    private String collect_status;

    private ReleaseFacilitiesAdapter releaseFacilitiesAdapter;
    private ShopLikeAdapter shopLikeAdapter;
    private List<AdvertisesmetListModel> advList = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_detail;
    }

    private void loadData() {

        service = RequestApi.create(RequestService.class);
        shopId = getIntent().getStringExtra("shopId");
        showDialog("loading");
        Call<ResponseData<ShopDetailsModel>> shopsSearchParams = service.getShopsInfo(shopId);
        shopsSearchParams.enqueue(new Callback<ResponseData<ShopDetailsModel>>() {
            @Override
            public void onResponse(Call<ResponseData<ShopDetailsModel>> call, Response<ResponseData<ShopDetailsModel>> response) {
                disMiss();
                if (response.body() != null) {
                    int resultCode = response.body().getResultCode();
                    if (resultCode == 200) {
                        model = response.body().getData();
                        setData();
                    } else {
                        showTextToast(response.body().getMessage());
                    }
                } else {
                    showTextToast("请求失败");
                }
            }

            @Override
            public void onFailure(Call<ResponseData<ShopDetailsModel>> call, Throwable t) {
                disMiss();
                showTextToast(RequestApi.detailError(t));
            }
        });
    }

    @Override
    public void initData() {

    }
    /**
     * 定位SDK监听函数
     */
    public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null || mMapView == null) {
                return;
            }
            myPt = new LatLng(location.getLatitude(), location.getLongitude());
            if (mLocClient.isStarted()) {
                // 获得位置之后停止定位
                mLocClient.stop();
            }
        }

        public void onReceivePoi(BDLocation poiLocation) {
        }
    }

    @Override
    public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {

    }

    @Override
    public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
        if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
            Toast.makeText(this, "抱歉，未能找到结果", Toast.LENGTH_LONG)
                    .show();
            return;
        }
        myMarker = (Marker) mBaiduMap.addOverlay(new MarkerOptions().position(result.getLocation())
                .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.banner_dot_select)));
        MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLngZoom(result
                .getLocation(), 17);
        mBaiduMap.setMapStatus(mapStatusUpdate);

    }
    private void initMap(){
        mSearch = GeoCoder.newInstance();
        mSearch.setOnGetGeoCodeResultListener(this);
        mLocClient = new LocationClient(this);
        mLocClient.registerLocationListener(myListener);
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setOpenGps(true); // 打开gps
        option.setCoorType("wgs84"); // 设置坐标类型
        option.setScanSpan(1000);
        mLocClient.setLocOption(option);
        mMapView. showScaleControl(false);
        mMapView. showZoomControls(false);
        mMapView.getMap().getUiSettings().setAllGesturesEnabled(false);
        mLocClient.start();

        mBaiduMap = mMapView.getMap();
        mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                Intent intent = new Intent(ShopDetailsActivity.this, MapActivity.class);
                intent.putExtra("shopLocation", shopInfo.getShopLocation());
                intent.putExtra("shopAddress", shopInfo.getShopTitle());
                intent.putExtra("area", shopInfo.getShopAddress());
                startActivity(intent);
            }

            @Override
            public boolean onMapPoiClick(MapPoi mapPoi) {
                return false;
            }
        });

    }
    private void showMap(){
        String[] split = shopInfo.getShopLocation().split(",");
        ptCenter = new LatLng(Float.parseFloat(split[0]), Float.parseFloat(split[1]));
        // 反Geo搜索
        mSearch.reverseGeoCode(new ReverseGeoCodeOption()
                .location(ptCenter));
    }
    private void setData() {
        advList = model.getAdvertisesmetList();
        if (advList != null && advList.size() > 0) {
            banner.setImgUrlData(advList);
        }
        if (model.getLabelList() != null && model.getLabelList().size() > 0) {
            ArrayList<String> label = new ArrayList<>();
            for (int i = 0; i < model.getLabelList().size(); i++) {
                label.add(model.getLabelList().get(i).getLabName());
            }
            labelsView.setLabels(label); //直接设置一个字符串数组就可以了。
        }
        if (model.getLabelList1() != null && model.getLabelList1().size() > 0) {
            releaseFacilitiesAdapter = new ReleaseFacilitiesAdapter(model.getLabelList1(), this);
            sc_labelList1.setAdapter(releaseFacilitiesAdapter);
        }
        if (model.getShopRecommendedList() != null && model.getShopRecommendedList().size() > 0) {
            shopLikeAdapter = new ShopLikeAdapter(model.getShopRecommendedList(), this);
            sc_like.setAdapter(shopLikeAdapter);
        }
        shopkeeperInfo = model.getShopkeeperInfo();
        if(shopkeeperInfo!=null){
            Glide.with(this)
                    .load(shopkeeperInfo.getShopkeeperHeadimage())
                    .centerCrop()
                    .error(R.drawable.img_default)
                    .into(img_photo);
            tv_name.setText(shopkeeperInfo.getShopkeeperName());
            tv_no.setText(shopkeeperInfo.getShopkeeperCount());
        }
        shopInfo = model.getShopInfo();
        if (shopInfo != null) {
            showMap();

            tv_update_time.setText(shopInfo.getCreateTimeText());
            tv_shopRentType.setText(shopInfo.getShopRentType());
            tv_shopRentType_1.setText(shopInfo.getShopRentType());
            if ("月租金".equals(shopInfo.getShopRentType())) {
                tv_shopRent.setText(shopInfo.getShopRent() + "元/月");
                tv_shopRent_1.setText(shopInfo.getShopRent() + "元/月");
            } else {
                tv_shopRent.setText(shopInfo.getShopRent() + "万元/年");
                tv_shopRent_1.setText(shopInfo.getShopRent() + "万元/年");
            }
            tv_shopTransferFee.setText(shopInfo.getShopTransferFee() + "万元");
            tv_shopAvailableMeasure.setText(shopInfo.getShopBuildMeasure() + "㎡");
            tv_shopNo.setText("商铺编号：" + shopInfo.getShopNo());
            tv_address.setText(shopInfo.getArea() + shopInfo.getShopStreet() + shopInfo.getShopAddress());
            tv_shopIntroduce.setText(shopInfo.getShopIntroduce());
            tv_shopType.setText(shopInfo.getShopType());
            if ("0".equals(shopInfo.getShopSaleState())) {
                tv_shopSaleState.setText("租赁中");
            } else {
                tv_shopSaleState.setText("已租赁");
            }
            if ("0".equals(shopInfo.getIsVip())) {
                tv_call_shop.setVisibility(View.GONE);
            } else {
                tv_call_shop.setVisibility(View.VISIBLE);
            }
            tv_shopFaceWidth.setText(shopInfo.getShopFaceWidth()+"米");
            tv_shopFloor.setText(shopInfo.getShopFloor());
            tv_shopCurrentBusiness.setText(shopInfo.getShopCurrentBusiness());
            tv_area.setText(shopInfo.getArea());
            tv_shopBusinessArea.setText(shopInfo.getShopBusinessArea());
            tv_shopAddress.setText(shopInfo.getShopAddress());
            tv_shopStreet.setText(shopInfo.getShopStreet());
            tv_shopPropertiyRight.setText(shopInfo.getShopPropertiyRight());
            tv_shopBuildMeasure.setText(shopInfo.getShopBuildMeasure() + "㎡");
            tv_shopAvailableMeasure_1.setText(shopInfo.getShopAvailableMeasure() + "㎡");
            tv_shopDepth.setText(shopInfo.getShopDepth()+"米");
            tv_shopFloorHeight.setText(shopInfo.getShopFloorHeight()+"米");
            if ("0".equals(shopInfo.getShopCut())) {
                tv_shopCut.setText("否");
            } else {
                tv_shopCut.setText("是");
            }
            tv_shopRunState.setText(shopInfo.getShopRunState());
            tv_shopRunYears.setText(shopInfo.getShopRunYears());
            tv_shopBrandState.setText(shopInfo.getShopBrandState());
            tv_shopDeposit.setText(shopInfo.getShopDeposit());
            tv_shopRunIndustry_1.setText(shopInfo.getShopRunIndustry());
            tv_shopCurrentLeaseYears.setText(shopInfo.getShopCurrentLeaseYears());
            tv_shopSurplusYears.setText(shopInfo.getShopSurplusYears());
            tv_shopLimitYears.setText(shopInfo.getShopLimitYears());
            tv_shopRenew.setText(shopInfo.getShopRenew());
            tv_shopTransferFee_1.setText(shopInfo.getShopTransferFee() + "万元");
            tv_shopTransferContent.setText(shopInfo.getShopTransferContent());
            tv_shop_title.setText(shopInfo.getShopTitle());
            tv_shopProfit.setText(shopInfo.getShopProfit());
            tv_leaseType.setText(shopInfo.getLeaseType());
            webView.loadUrl(shopInfo.getShopReport());
//            webView.loadUrl("https://www.baidu.com");
            if (isLogin()) {
                collect_status = shopInfo.getShopCollection();
                if ("0".equals(collect_status)) {
                    tv_collect.setText("收藏店铺");
                    img_collect.setImageResource(R.drawable.img_un_collect);
                } else {
                    tv_collect.setText("取消收藏");
                    img_collect.setImageResource(R.drawable.img_collected);
                }
            } else {
                tv_collect.setText("收藏店铺");
                img_collect.setImageResource(R.drawable.img_un_collect);
            }
        }

    }

    private void setWebView() {
        WebSettings webSettings = webView.getSettings();
        webSettings.setUseWideViewPort(false);  //将图片调整到适合webview的大小
        webSettings.setDomStorageEnabled(true);//设置适应Html5的一些方法
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); //支持内容重新布局
        webSettings.setJavaScriptEnabled(true);

        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                // showDialog("loading");
            }

            @Override
            public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
                super.onReceivedHttpError(view, request, errorResponse);
                // disMiss();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                // disMiss();
//                LinearLayout.LayoutParams  params = (LinearLayout.LayoutParams) webView.getLayoutParams();
//                params.width = DisplayUtil.getMobileWidth(ShopDetailsActivity.this);
//                params.height = DisplayUtil.getMobileHeight(ShopDetailsActivity.this);
//                webView.setLayoutParams(params);
            }
        });
    }

    @Override
    public void initView() {
        initMap();
        setWebView();
        int statusBarHeight1 = -1;
//获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
        }
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(title.getLayoutParams());
        //lp.setMargins(0, statusBarHeight1, 0, 0);
        lp.height = statusBarHeight1 + DipUtil.dip2px(this, 50);
        title.setLayoutParams(lp);
        title.setPadding(0, statusBarHeight1, 0, 0);
        lin_title_middle.setPadding(0, statusBarHeight1, 0, 0);
        title.getBackground().setAlpha(0);
        title.setVisibility(View.GONE);
        rel_jyzk.setOnClickListener(this);
        rel_pyxx.setOnClickListener(this);
        rel_pybg.setOnClickListener(this);
        rel_left.setOnClickListener(this);
        rel_collect.setOnClickListener(this);
        lin_address.setOnClickListener(this);
        tv_call.setOnClickListener(this);
        tv_call_pbb.setOnClickListener(this);
        tv_call_shop.setOnClickListener(this);
        rel_call.setOnClickListener(this);
        sc_like.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ShopListModel shopListModel = model.getShopRecommendedList().get(position);
                String shopId = shopListModel.getShopId();
                startActivity(new Intent(ShopDetailsActivity.this, ShopDetailsActivity.class).putExtra("shopId", shopId));
            }
        });
        banner.setOnHeaderViewClickListener(new BannerHeaderDetailView.HeaderViewClickListener() {
            @Override
            public void HeaderViewClick(int position) {
                Intent intent = new Intent(ShopDetailsActivity.this, PhotoActivity.class);
                intent.putExtra("postion", position);
                intent.putExtra("advList", (Serializable) advList);
                startActivity(intent);
            }
        });
        scroll.setScrollViewListener(new MyScrollView.ScrollViewListener() {

            @Override
            public void onScrollChanged(View scrollView, int x, int y,
                                        int oldx, int oldy) {
                // TODO Auto-generated method stub
                System.out.println("y:" + y + " oldy:" + oldy + " scroll:" + scroll.getScrollY());
                titleAnima(y);
            }

            @Override
            public void onGoOnMove() {

            }

            @Override
            public void onReflash() {

            }

        });
    }
    @Override
    public void onPause() {
        mMapView.onPause();
        super.onPause();
    }

    @Override
    public void onResume() {
        RxPermissions.getInstance(this)
                .request(
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION)
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean aBoolean) {
                        if (aBoolean) {

                        } else {
                            showTextToast("请选择允许使用权限");
                        }
                    }
                });
        setStatusWhite(false);
        loadData();
        mMapView.onResume();
        super.onResume();
    }

    @Override
    public void onDestroy() {
        mMapView.onDestroy();
        // 退出时销毁定位
        mLocClient.stop();
        // 关闭定位图层
        mBaiduMap.setMyLocationEnabled(false);
        super.onDestroy();
    }
    /**
     * 出现渐变效果
     */
    public void titleAnima(int y) {
        if (y == 0) {
            title.setVisibility(View.GONE);
        } else {
            title.setVisibility(View.VISIBLE);
        }
        int scrollHeight = banner.getHeight();
        float scrollPercent = (float) y / scrollHeight;
        if (y > scrollHeight) {
            scrollPercent = 1;
        }
        title.getBackground().setAlpha((int) (255 * scrollPercent));
    }

    private void setStatus(TextView tv, LinearLayout lin) {
        tv_pyxx.setTextColor(getResources().getColor(R.color.grey));
        tv_pybg.setTextColor(getResources().getColor(R.color.grey));
        tv_jyzk.setTextColor(getResources().getColor(R.color.grey));
        lin_jyzk.setVisibility(View.GONE);
        lin_pybg.setVisibility(View.GONE);
        lin_pyxx.setVisibility(View.GONE);

        lin.setVisibility(View.VISIBLE);
        tv.setTextColor(getResources().getColor(R.color.purple));
    }

    private void setCollect(final String type) {
        showDialog("loading");
        Call<ResponseData> responseDataCall = service.shopCollect(shopId, type);
        responseDataCall.enqueue(new Callback<ResponseData>() {
            @Override
            public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
                disMiss();
                if (response.body() != null) {
                    int resultCode = response.body().getResultCode();
                    if (resultCode == 200) {
                        if ("0".equals(type)) {
                            collect_status = "1";
                            showTextToast(response.body().getMessage());
                            tv_collect.setText("取消收藏");
                            img_collect.setImageResource(R.drawable.img_collected);
                        } else {
                            collect_status = "0";
                            showTextToast(response.body().getMessage());
                            tv_collect.setText("收藏店铺");
                            img_collect.setImageResource(R.drawable.img_un_collect);
                        }
                    } else {
                        showTextToast(response.body().getMessage());
                    }
                } else {
                    showTextToast("请求失败");
                }
            }

            @Override
            public void onFailure(Call<ResponseData> call, Throwable t) {
                disMiss();
                showTextToast(RequestApi.detailError(t));
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rel_call:
                AlertDialog.Builder shopkeeperPhone_dialog = new AlertDialog.Builder(this)
                        .setTitle("温馨提示")
                        .setMessage(shopkeeperInfo.getShopkeeperPhone())
                        .setPositiveButton("呼叫", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                RxPermissions.getInstance(ShopDetailsActivity.this)
                                        .request(
                                                Manifest.permission.CALL_PHONE)
                                        .subscribe(new Action1<Boolean>() {
                                            @Override
                                            public void call(Boolean aBoolean) {
                                                if (aBoolean) {
                                                    IntentUtil.dial(ShopDetailsActivity.this, shopkeeperInfo.getShopkeeperPhone());
                                                } else {
                                                    showTextToast("请选择允许使用权限");
                                                }
                                            }
                                        });
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });

                shopkeeperPhone_dialog.create().show();
                break;
            case R.id.tv_call_shop:
                String phone = "400-928-2858";
                if (shopInfo != null) {
                    phone = shopInfo.getShopOwnPhone();
                }
                final String finalPhone = phone;
                AlertDialog.Builder dialogBuilder_shop = new AlertDialog.Builder(this)
                        .setTitle("温馨提示")
                        .setMessage(finalPhone)
                        .setPositiveButton("呼叫", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                RxPermissions.getInstance(ShopDetailsActivity.this)
                                        .request(
                                                Manifest.permission.CALL_PHONE)
                                        .subscribe(new Action1<Boolean>() {
                                            @Override
                                            public void call(Boolean aBoolean) {
                                                if (aBoolean) {
                                                    IntentUtil.dial(ShopDetailsActivity.this, finalPhone);
                                                } else {
                                                    showTextToast("请选择允许使用权限");
                                                }
                                            }
                                        });
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });

                dialogBuilder_shop.create().show();
                break;
            case R.id.tv_call_pbb:
                AlertDialog.Builder dialogBuilder_pbb = new AlertDialog.Builder(this)
                        .setTitle("温馨提示")
                        .setMessage("400-928-2858")
                        .setPositiveButton("呼叫", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                RxPermissions.getInstance(ShopDetailsActivity.this)
                                        .request(
                                                Manifest.permission.CALL_PHONE)
                                        .subscribe(new Action1<Boolean>() {
                                            @Override
                                            public void call(Boolean aBoolean) {
                                                if (aBoolean) {
                                                    IntentUtil.dial(ShopDetailsActivity.this, "400-928-2858");
                                                } else {
                                                    showTextToast("请选择允许使用权限");
                                                }
                                            }
                                        });
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });

                dialogBuilder_pbb.create().show();
                break;
            case R.id.tv_call:
                AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this)
                        .setTitle("温馨提示")
                        .setMessage("400-928-2858")
                        .setPositiveButton("呼叫", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                RxPermissions.getInstance(ShopDetailsActivity.this)
                                        .request(
                                                Manifest.permission.CALL_PHONE)
                                        .subscribe(new Action1<Boolean>() {
                                            @Override
                                            public void call(Boolean aBoolean) {
                                                if (aBoolean) {
                                                    IntentUtil.dial(ShopDetailsActivity.this, "400-928-2858");
                                                } else {
                                                    showTextToast("请选择允许使用权限");
                                                }
                                            }
                                        });
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });

                dialogBuilder.create().show();
                break;
            case R.id.lin_address:
//                RxPermissions.getInstance(this)
//                        .request(
//                                Manifest.permission.ACCESS_COARSE_LOCATION,
//                                Manifest.permission.ACCESS_FINE_LOCATION)
//                        .subscribe(new Action1<Boolean>() {
//                            @Override
//                            public void call(Boolean aBoolean) {
//                                if (aBoolean) {
//                                    Intent intent = new Intent(ShopDetailsActivity.this, MapActivity.class);
//                                    intent.putExtra("shopLocation", shopInfo.getShopLocation());
//                                    intent.putExtra("shopAddress", shopInfo.getShopTitle());
//                                    intent.putExtra("area", shopInfo.getShopAddress());
//                                    startActivity(intent);
//                                } else {
//                                    showTextToast("请选择允许使用权限");
//                                }
//                            }
//                        });
                break;
            case R.id.rel_collect:
                if (isLogin()) {
                    setCollect(collect_status);
                } else {
                    startActivity(LoginActivity.class);
                }
                break;
            case R.id.rel_left:
                finish();
                break;
            case R.id.rel_pyxx:
                setStatus(tv_pyxx, lin_pyxx);
                break;
            case R.id.rel_pybg:
                webView.reload();
                setStatus(tv_pybg, lin_pybg);
                break;
            case R.id.rel_jyzk:
                setStatus(tv_jyzk, lin_jyzk);
                break;
        }
    }
}
